﻿var $edittr = null;
var ServiceId = GetQueryString("Id");
$(function () {
    $("#DataServiceId").val(ServiceId);
    getServiceDetails();
    GetEnumeratePubDataSource();

    $("#AddDataSource").validate({
        submitHandler: function () {
            try {
                var parstr = "";
                var parameterlist = $("#table-details-parameter tbody tr");
                for (var j = 0; j < parameterlist.length; j++) {
                    var tempjson1 = JSON.parse($(parameterlist[j]).attr("data"));
                    parstr += (tempjson1.name + ",");
                    parstr += (tempjson1.type + ",");
                    parstr += (tempjson1.defaultValue + ",");
                    parstr += (tempjson1.description);
                    if (j != (parameterlist.length - 1)) {
                        parstr += ",";
                    }
                }
                $("#ParameterSettings").val(parstr);
                var url = "DataSourceDetails-DataService.html?Id=" + ServiceId;
                //新增
                $.ajax({
                    url: apiUrl,
                    type: "Post",
                    dataType: "json",
                    data: $("#AddDataSource").serialize(),
                    success: function (data) {
                        if (data.IsSccuess) {
                            ShowGritter("保存成功!", 2, url);
                        } else {
                            if (IsLogined(data.ErrorCode)) {
                                alert("新增失败（" + "错误代码：" + data.ErrorCode + "，错误信息：" + data.Message + ")");
                            }
                        }
                    },
                    beforeSend: function () {
                        ShowGritter("正在提交");
                    }
                });

            } catch (e) {
                console.log(e);
            }
        }
    });
    $("#Chose-Form").submit(function () {
        var $this = $(".chosevalue:checked");
        $("#Value").val($this.val());
        $("#Value-Text").val($this.parent().prev().prev().html());
        GetEnumeratePubParameter($this.val());
        $("#Chose-Div").modal("hide");
        return false;
    });
});
//组合参数数据  编辑时需调用此方法处理返回的数据
//传入的参数类型为对象数组
var AppadParameterData = function (data) {
    //var list = new Array();
    var trlist = "";
    for (var i = 0; i < data.length; i++) {
        var $this = data[i];
        trlist += AssembleParameterData({ name: $this.Name, id: $this.Id, type: $this.Type, defaultValue: "", description: $this.Description });
    }
    $("#table-details-parameter tbody").html(trlist);
}
//传入参数对象返回一行的表格数据
var AssembleParameterData = function (data) {
    var tr = "<tr data='" + JSON.stringify(data) + "'><td>";
    tr += (data.name + "</td>");
    tr += ("<td>" + data.type + "</td>");
    tr += ("<td>" + data.defaultValue + "</td>");//默认值
    tr += ("<td>" + data.description + "</td></tr>");//备注
    return tr;
}

//获取服务的详细信息
var getServiceDetails = function () {
    $.ajax({
        url: apiUrl,
        data: {
            "sys:sno": "ETL",
            "sys:act": "GetDataService",
            "DataServiceId": ServiceId
        },
        dataType: "json",
        success: function (data) {
            if (data.IsSccuess) {
                $("#ServiceName").val(data.Data.Name);
            } else {
                if (IsLogined(data.ErrorCode)) {
                    alert(data.Message);
                }
            }
        },
        beforeSend: function () {
            // $("btn_login").html("登录中...");
        }
    });
}
//传入的参数类型为对象数组
var GetEnumeratePubDataSource = function () {

    $.ajax({
        url: apiUrl,
        data: {
            "sys:sno": "DS",
            "sys:act": "EnumeratePubDataSource",
            "DataServiceId": ServiceId
        },
        dataType: "json",
        success: function (data) {
            if (data.IsSccuess) {
                var trlist = "";
                for (var i = 0; i < data.Data.Items.length; i++) {
                    var $this = data.Data.Items[i];
                    trlist += ("<tr><td>" + $this.Name + "</td>");
                    trlist += ("<td>" + $this.Description + "</td>");
                    trlist += ("<td><input type='radio' name='chosevalue' class='chosevalue'  value='" + $this.Value + "'/></td></tr>");
                }
                $("#table-details-value").append(trlist);
            } else {
                if (IsLogined(data.ErrorCode)) {
                    alert(data.Message);
                }
            }
        }
    });

}
//传入的参数类型为对象数组
var GetEnumeratePubParameter = function (dataSourceId) {
    $.ajax({
        url: apiUrl,
        data: {
            "sys:sno": "DS",
            "sys:act": "EnumeratePubParameter",
            "DataServiceId": ServiceId,
            "DataSource": dataSourceId
        },
        dataType: "json",
        success: function (data) {
            if (data.IsSccuess) {
                AppadParameterData(data.Data.Items);
            } else {
                if (IsLogined(data.ErrorCode)) {
                    alert(data.Message);
                }
            }
        },
        beforeSend: function () {
            // $("btn_login").html("登录中...");
        }
    });
}

